#include "onlinemusicwidget.h"
#include "ui_onlinemusicwidget.h"
#include <QPixmap>
#include <QPainter>
#include <QFileDialog>
#include <QDir>
#include <QMediaPlayer>
#include <QAudioOutput>
#include <QUrl>
OnlineMusicWidget::OnlineMusicWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::OnlineMusicWidget)
{
    ui->setupUi(this);
    this->resize(400,600);
    ui->verticalSlider->hide();
    //加载模块multimedia
    //先new一个output对象
    audioOutput= new QAudioOutput(this);
    //new一个媒体播放对象
    mediaPlayer= new QMediaPlayer(this);
    mediaPlayer->setAudioOutput(audioOutput);

    //获取当前媒体时长，通过信号与槽获取
    connect(mediaPlayer,&QMediaPlayer::durationChanged,this,[=](qint64 duration){
        ui->totalLabel->setText(QString("%1:%2").arg(duration/1000/60,2,10,QChar('0')).arg(duration/1000%60));
        ui->playCourseSlider->setRange(0,duration);//设置进度滑块范围
    });
    //获取当前播放时长
    connect(mediaPlayer,&QMediaPlayer::positionChanged,this,[=](qint64 pos){
        ui->curLabel->setText(QString("%1:%2").arg(pos/1000/60,2,10,QChar('0')).arg(pos/1000%60,2,10,QChar('0')));
        ui->playCourseSlider->setValue(pos);//设置进度滑块根据播放时长滑动
    });
    //拖动滑块，改变播放进度
    connect(ui->playCourseSlider,&QSlider::sliderMoved,mediaPlayer,&QMediaPlayer::setPosition);
    //设置音量
}

OnlineMusicWidget::~OnlineMusicWidget()
{
    delete ui;
}

void OnlineMusicWidget::paintEvent(QPaintEvent *ev)
{
    QPainter painter(this);
    painter.drawPixmap(0,0,width(),height(),QPixmap(":/new/prefix1/image/beijing.jpg"));
}

void OnlineMusicWidget::on_pushButton_3_clicked()
{
    //关闭窗口
    close();
}


void OnlineMusicWidget::on_pushButton_4_clicked()
{
    //打开文件对话框
     auto path=QFileDialog::getExistingDirectory(this,"请选择你的音乐所在位置","C:/qtxm/OnlineMusic/music");
    //根据路径获取mp3、wav文件
     QDir dir(path);
     auto musicList=dir.entryList(QStringList()<<"*.mp3"<<"*.wav");
     //把音乐放到listwidget展示
     ui->listWidget->addItems(musicList);
     //默认选中第一个音乐
     ui->listWidget->setCurrentRow(0);
     //将音乐路径保存起来
     for(auto file: musicList)
     {
         playList.append(QUrl::fromLocalFile(path+"/"+file));

     }
}


void OnlineMusicWidget::on_pushButton_7_clicked()
{
    if(playList.empty())
    {
        return;
    }
    switch(mediaPlayer->playbackState())
    {
    case QMediaPlayer::PlaybackState::StoppedState://停止状态
    {
        //如果没有播放，就播放当前选中的音乐
        //记录行号
        curPlayIndex=ui->listWidget->currentRow();
        //播放对应下标的音乐
        mediaPlayer->setSource(playList[curPlayIndex]);
        mediaPlayer->play();
        break;
    }
    case QMediaPlayer::PlaybackState::PlayingState://播放状态
        //如果正在播放，就暂停
        mediaPlayer->pause();
        break;
    case QMediaPlayer::PlaybackState::PausedState://暂停状态
        //如果暂停那就继续播放音乐
        mediaPlayer->play();
        break;
    }






}


void OnlineMusicWidget::on_pushButton_6_clicked()
{
    //上一曲
    if(curPlayIndex==0)curPlayIndex=playList.size()-1;
    else curPlayIndex=curPlayIndex-1;
    ui->listWidget->setCurrentRow(curPlayIndex);
    mediaPlayer->setSource(playList[curPlayIndex]);
    mediaPlayer->play();
}


void OnlineMusicWidget::on_pushButton_8_clicked()
{
    //下一曲
    curPlayIndex=(curPlayIndex+1)%playList.size();
    ui->listWidget->setCurrentRow(curPlayIndex);
    mediaPlayer->setSource(playList[curPlayIndex]);
    mediaPlayer->play();
}


void OnlineMusicWidget::on_listWidget_doubleClicked(const QModelIndex &index)
{
    curPlayIndex=index.row();
    mediaPlayer->setSource(playList[curPlayIndex]);
    mediaPlayer->play();
}


void OnlineMusicWidget::on_pushButton_9_clicked()
{
    if(!kj){
        ui->verticalSlider->show();
        kj=true;
        ui->verticalSlider->setValue(savedVolumeValue);
        ui->verticalSlider->setRange(0,100);
        connect(ui->verticalSlider,&QSlider::sliderMoved,audioOutput,&QAudioOutput::setVolume);
    }
    else {
        ui->verticalSlider->hide();
        kj=false;
        savedVolumeValue = ui->verticalSlider->value();
    }
}


void OnlineMusicWidget::on_verticalSlider_valueChanged(int value)
{
    audioOutput->setVolume(value/100.0);
}

